<?php

$db 	= new Kabupaten();
$table 	= "kabkota";
$order	= "";
$id 	= htmlentities(mysql_real_escape_string($_GET['idkabkota']));
$kabupaten = $db->lihat($table,$order,$id);


if ($kabupaten) { 
foreach ($kabupaten as $x) {

?>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script src="../assets/js/polygon.min.js"></script> 
<script type="text/javascript">
	var peta;
	var gambar_tanda;
	gambar_tanda = '../assets/img/pointer.png';
	<?php
	$str = $x['polygon'];
	$str = str_replace(" ","", $str);
	$str = str_replace(")(",") (", $str);
	$str = explode(" ", $str);

	for ($i=0;$i<count($str);$i++) {
		$cord .= "new google.maps.LatLng".$str[$i].",";
	}
	$cord = substr($cord,0,strlen($cord)-1);
	
	
	?>
	var paths = [<?php echo $cord; ?>];
	function peta_awal(){
	    var lokasibaru = new google.maps.LatLng(<?php echo $x['lat'].",".$x['long']; ?>);
    	var petaoption = {
			zoom: 8,
			center: lokasibaru,
			mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
	    peta = new google.maps.Map(document.getElementById("map_canvas"),petaoption);
	    
		poly = new google.maps.Polygon({
				paths: paths,
				strokeWeight: 0,
				fillColor: "#ff0000",
				fillOpacity: 0.5
		});
			
		poly.setMap(peta);
				
	    tanda = new google.maps.Marker({
	        position: lokasibaru,
	        map: peta, 
	        icon: gambar_tanda,
	        draggable : true
	    });
	    
	     var creator = new PolygonCreator(peta);
		 
		 //reset
		 $('#reset').click(function(){ 
		 		creator.destroy();
		 		creator=null;
		 		creator=new PolygonCreator(peta);
		 });		 
		 
		 
		 //show paths
		 $('#showData').click(function(){ 
		 		$('#dataPanel').empty();
		 		if(null==creator.showData()){
		 			$('#dataPanel').append('Please first create a polygon');
		 		}else{
		 			$('#dataPanel').append(creator.showData());
		 		}
		 });
	    
		 
	    google.maps.event.addListener(tanda, 'dragend', function(event){
				document.getElementById('latitude').value = this.getPosition().lat();
				document.getElementById('longitude').value = this.getPosition().lng();
		});
	}
</script>

<?php

   if (isset($_GET['success']) && ($_GET['success'] == "1")) {

		$o .= '<div class="alert alert-success">
				Proses tambah kabupaten berhasil
				</div>';
				
	} elseif (isset($_GET['success']) && ($_GET['success'] == "0")) {
		
		$o .= '<div class="alert alert-error">
				Proses tambah kabupaten gagal
			   </div>';
	}
	

$o .= '
<div class="row">
	<div class="span8">
		<div class="control-group">
			<div id="map_canvas" style="width:100%; height:500px"></div>
		</div>
		<a href="?page=kabupaten"><i class=" icon-arrow-left"></i> kembali</a>
	</div>';
	

		$o .= '
			<div class="span4">
			<form class="form-horizontal" id="registerHere" method="post" action="?page=editkab&idkabkota='.$id.'&action=edit">
			 <fieldset>
				<legend>UBAH KABUPATEN</legend>
				<div class="control-group">
				  <label class="control-label" for="input01">Kabupaten</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['nama_kabkota'].'" id="kabupaten" name="kabupaten" rel="popover" data-content="Masukkan nama kabupaten." data-original-title="Kabupaten">
				  </div>
				</div>

				<div class="control-group">
				<label class="control-label" for="input01">Luas Wilayah</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['luas_wilayah'].'" id="luas_wilayah" name="luas_wilayah" rel="popover" data-content="Masukkan luas wilayah" data-original-title="Luas_wilayah">
				   
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Kepadatan Penduduk</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['kepadatan_penduduk'].'" id="kepadatan_penduduk" name="kepadatan_penduduk" rel="popover" data-content="Masukkan jumlah kepadatan penduduk" data-original-title="Kepadatan Penduduk">
				   
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Tata Guna Lahan</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['tata_guna_lahan'].'" id="tata_guna_lahan" name="tata_guna_lahan" rel="popover" data-content="Masukkan tata guna lahan" data-original-title="Tata Guna Lahan">
				   
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Topografi Wilayah</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['topografi_wilayah'].'" id="tofografi_wilayah" name="tofografi_wilayah" rel="popover" data-content="Masukkan jumlah topografi wilayah" data-original-title="Topografi Wilayah">
				   
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Sumber Air Bersih</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['sumber_air_bersih'].'" id="sumber_air_bersih" name="sumber_air_bersih" rel="popover" data-content="Masukkan jumlah sumber air bersih" data-original-title="Sumber Air Bersih">
				   
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Longitude</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['long'].'" id="longitude" name="longitude" rel="popover" data-content="Masukkan koordinat" data-original-title="Longitude">
				   
				  </div>
				</div>
				
				<div class="control-group">
				<label class="control-label" for="input01">Latitude</label>
				  <div class="controls">
					<input type="text" class="input-xlarge" value="'.$x['lat'].'" id="latitude" name="latitude" rel="popover" data-content="Masukkan koordinat" data-original-title="Latitude">
				   
				  </div>
				</div>
				
					<div class="control-group">
				<label class="control-label" for="input01">Polygon</label>
				  <div class="controls">
					<textarea class="input-xlarge" id="dataPanel" name="polygon">'.$x['polygon'].'</textarea>
					<button id="showData" type="button" class="btn btn-primary">Tampilkan Koordinat</button>
					<button id="reset" value="Reset" type="button" class="btn btn-warning">Reset Polygon</button>
				  </div>
			</div>
				<div class="control-group">
					<label class="control-label" for="input01"></label>
					  <div class="controls">
					   <button type="submit" class="btn btn-success" >Ubah Kabupaten</button>
					  </div>
				</div>
				  </fieldset>
			</form>
				</div>
		</div>';
	}
}

echo $o;

if ($_GET['action'] == "edit") {
	
	/**
		$nama_kab	= htmlentities(mysql_real_escape_string($_POST['kabupaten']));
		$wilayah		= htmlentities(mysql_real_escape_string($_POST['luas_wilayah']));
		$lat				= htmlentities(mysql_real_escape_string($_POST['latitude']));
		$long			= htmlentities(mysql_real_escape_string($_POST['longitude']));
	**/
	
	$db = new Kabupaten();
	$id = $_GET['idkabkota'];
	$kab = array('nama_kabkota'=>$_POST['kabupaten'],'luas_wilayah'=>$_POST['luas_wilayah'],'lat'=>$_POST['latitude'],'long'=>$_POST['longitude'],'polygon'=>$_POST['polygon']);
	$kabkota = $db->ubah($id,$table,$kab);

	if ($kabkota) {
		echo '
			<script language="javascript">
			document.location="?page=editkab&idkabkota='.$id.'&success=1";
			</script>
		';
		
	} else {
		echo '
				<script language="javascript">
				document.location="?page=editkab&idkabkota='.$id.'&success=0";
				</script>
			';	
		
	}
	
}





